package com.jjc.android.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.jjc.android.*
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.PrivacyDialog
import com.jjc.android.util.GlobalActivityStack
import com.jjc.android.util.UIStatusBarHelper
import com.lxj.xpopup.XPopup

class SplashActivity : BaseActivity() {

    private lateinit var imageView:ImageView
    private lateinit var versionNameTxt: TextView
    private lateinit var appNameTxt: TextView
    private lateinit var iconImage: ImageView
    override val layoutId: Int = R.layout.activity_splash

    @SuppressLint("CheckResult")
    override fun initView(savedInstanceState: Bundle?) {
        if(!isTaskRoot){
            finish()
            return
        }
        GlobalManager.fetchAppInfo()
        initViews()
        if(Constants.IS_SJ_VERSION||UserManager.isPopuAgreement){
            imageView.postDelayed(Runnable {
                handleJump()
            },1000)
        }else{
            XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(PrivacyDialog(this){ success->
                    if(success){
                        handleJump()
                    }else{
                        GlobalActivityStack.finishAll()
                    }
                })
                .show()
        }

    }

    private fun handleJump(){
        if(Constants.IS_SJ_VERSION){
            startActivity(Intent(this,InspectionVersionActivity::class.java))
        }else{
        if(UserManager.isShowGuide){
            if(!UserManager.toLogin(this)){
             MainActivity.start(this)
            }
        }else{
            GuideActivity.start(this)
        }
        }
        finish()
    }

    private fun initViews() {
        imageView = findViewById(R.id.imageView)
        iconImage = findViewById(R.id.iconImage)
        appNameTxt = findViewById(R.id.appNameTxt)
        versionNameTxt = findViewById(R.id.versionNameTxt)
        versionNameTxt.text = String.format("Va%d",BuildConfig.VERSION_CODE)
    }

}